
clear all


** Use these values to exclude unexpected expenses from liquidity shocks

local shock_list a b c d e f g i j // exclude "unexpected expenses"
local svname noexpense


** Use these values to include unexpected expenses from liquidity shocks
/*
local shock_list a b c d e f g h i j // include "unexpected expenses"
local svname expense
*/

********************************************************************************
** Bring in the data and keep only CD defaulters

use "$DATA_OUT/NMDB_ASMB_respondents"
keep if in_default_sample==1
keep if x27==1
drop if survey_year==2016 // 2016 survey did not ask about retirement or payments on other mortgages
count // get sample size for figure footnote

svyset [pweight = analysis_weight]

** Count the number of issues listed by respondents as contributing to CD default
gen num_issues = 0
foreach var in `shock_list' {
	
	replace num_issues = num_issues + 1 if x31`var'==1
}

** Get the number of issues, and the number of respondents with >=3 issues
gen num_issues_capped     = num_issues
replace num_issues_capped = 3 if num_issues>2 & !mi(num_issues)

svy: tab num_issues
svy: tab num_issues_capped 


********************************************************************************
** Collapse the data and prepare the figure
foreach shock in `shock_list' {
	
	gen x31`shock'_0 = 100 * (x31`shock'==1) if !mi(x31`shock') //track whether the shock is reported
	gen x31`shock'_1 = x31`shock'_0 * (num_issues==1) // track whether the shock is the only one reported
	gen x31`shock'_2 = x31`shock'_0 * (inlist(num_issues,1,2)) //track whether the shock is one of at most two reported
	
	local cvars `cvars' x31`shock'_0 x31`shock'_1 x31`shock'_2
	local rvars `rvars' x31`shock'_
	
}	
	
collapse (mean) `cvars' [pweight = analysis_weight]

gen one = 1
reshape long `rvars', i(one) j(temp)
drop one temp


xpose, clear varname


rename v1 All
rename v2 JustOne
rename v3 OneOrTwo

gen reason = "Job loss" if _varname=="x31a_"
replace reason = "Payments on other large debts" if _varname=="x31j_"
replace reason = "Illness, disability, or death" if _varname=="x31e_"
replace reason = "Change in mortgage payments" if _varname=="x31g_"
replace reason = "Separation or divorce" if _varname=="x31d_"
replace reason = "Business failure" if _varname=="x31c_"
replace reason = "Retirement" if _varname=="x31b_"
replace reason = "Disaster affecting the property" if _varname=="x31f_"
replace reason =  "Payments on other mortgages"  if _varname=="x31i_"
replace reason =  "Unexpected expenses"  if _varname=="x31h_"

drop _varname
order reason


gsort - OneOrTwo
gen rank = _n



graph hbar JustOne OneOrTwo , over(reason, sort(rank)) ytitle("%") yscale(range(0 30)) ylabel(0(10)30) blabel(bar, format(%9.1f) size(tiny)) legend(label(1 "Just One Shock") label(2 "One or Two Shocks") position(6) cols(1) on) graphregion(color(white)) bgcolor(white) 
graph export "$OUTPUT/adverse_shocks_on_their_own_`svname'.pdf", replace


